package irc.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

import irc.view.FontView;

/** This class handles the font info and store the font types
 * @author Dybdal
 * @version 1.0
 * @
 */

public class FontController {
	public FontView font;
	private Color selected = new Color(0,0,0);
	private int size = 12;
	private String name = "Dialog";
	private int style = 0;
	private boolean bolds = false;
	private boolean italics = false;
	private Style messages;
	private Style events;
	private Style MOTD;
	private Style highlights;
	public FontView fonts;

	/** Constructor that initalize the styles
	 * @author Dybdal
	 * @since 1.0
	 */
	public FontController() {
		fonts = new FontView(this);						// creates the class FontView
		defaultStyle();
	}


	/** Actionlisteners for the font view
	 * @author Dybdal
	 * @since 1.0
	 */
	public void fontActionlisteners(JButton ok, JButton cancel, JButton defaults, final JButton color, final JComboBox<String> combo, final JRadioButton message, final JRadioButton event, final JRadioButton highlight, final JCheckBox bold, final JCheckBox italic, final JLabel preview, final JSpinner fontSize) {

		//-------------------------------- ok button --------------------------------
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (message.isSelected()) { //checks if message radiobutten is selected
				    StyleConstants.setFontFamily(messages, name);
				    StyleConstants.setFontSize(messages, size); //sets the different attributs
				    StyleConstants.setForeground(messages, selected); // for the font style
				    StyleConstants.setBold(messages, bolds);
				    StyleConstants.setItalic(messages, italics);
					fonts.closeFrame();
				} else if (event.isSelected()) {//checks if event radiobutten is selected
					 StyleConstants.setFontFamily(events, name);
					    StyleConstants.setFontSize(events, size);//sets the different attributs
					    StyleConstants.setForeground(events, selected);// for the font style
					    StyleConstants.setBold(events, bolds);
					    StyleConstants.setItalic(events, italics);
					fonts.closeFrame();
				} else if (highlight.isSelected()) {//checks if event radiobutten is selected
					 StyleConstants.setFontFamily(highlights, name);
					    StyleConstants.setFontSize(highlights, size);//sets the different attributs
					    StyleConstants.setForeground(highlights, selected);// for the font style
					    StyleConstants.setBold(highlights, bolds);
					    StyleConstants.setItalic(highlights, italics);
					fonts.closeFrame();
				} else {
					JOptionPane.showMessageDialog(null,	"You didnt select a text type!");
				}		//if no radiobutton i selected displey message
			}
		});

		//-------------------------------- cancel button --------------------------------
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fonts.closeFrame(); //close the frame
			}
		});

		//-------------------------------- font selector --------------------------------
		combo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				name = combo.getSelectedItem().toString(); // gets the selected object form combobox
				preview.setFont(new Font(name, style, size)); //set the preview font
				preview.updateUI();
			}
		});

		//-------------------------------- bold checkbox --------------------------------
		bold.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (bold.isSelected()) { //if bold is selected
					if (italic.isSelected()) { // if italic is selected
						style = style + Font.BOLD; // for the preview
					} else {
						style = Font.BOLD;
					}
					bolds = true; // sets bold to true
				} else {
					style = style - Font.BOLD; // for preview
					bolds = false;	//sets bold to false
				}
				preview.setFont(new Font(name, style, size));//set the preview font
				preview.updateUI();
			}
		});

		//-------------------------------- italic checkbox --------------------------------
		italic.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (italic.isSelected()) { //if italic is selected
					if (bold.isSelected()) { //if bold is selected
						style = style + Font.ITALIC; // for the preview
						
					} else {
						style = Font.ITALIC;
					}
					italics = true; //sets italic to true
				} else {
					style = style - Font.ITALIC; // for the preview
					italics = false; // sets italic to false
				}
				preview.setFont(new Font(name, style, size));//set the preview font
				preview.updateUI();
			}
		});

		//-------------------------------- color button --------------------------------
		color.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selected = JColorChooser.showDialog(null, // shows i dialog with colorselection
						"Choose new font color", color.getBackground());
				preview.setForeground(selected);//set the preview font
				preview.updateUI();
			}
		});

		//-------------------------------- font size selector --------------------------------
		fontSize.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int value = (Integer) fontSize.getValue(); // gets the size selected
				size = value; //sets size to selected value
				preview.setFont(new Font(name, style, size));//set the preview font
				preview.updateUI();
			}
		});
		
		defaults.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				defaultStyle();
			}
		});
		
	}

	/** Sets all font values to default
	 * @author Dybdal
	 * @since 1.0
	 */
	public void fontDefault()	{
		selected = new Color(0,0,0);
		size = 12;
		name = "Dialog";
		style = 0;
		bolds = false;
		italics = false;		
	}
	
	/** sets the default styles
	 * @author Dybdal
	 * @since 1.0
	 */
	public void defaultStyle()	{
		Color temp = new Color(50,205,50);
		Color temp1 = new Color(0,0,205);
		StyleContext sc = new StyleContext(); // gets style context
		Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE); // sets a deault style
		messages = sc.addStyle("MessageStyle", defaultStyle);		// crates a new style for messages
		events = sc.addStyle("EventStyle", defaultStyle);		// creates a new style for events
			StyleConstants.setFontFamily(events, "Dialog");
			StyleConstants.setFontSize(events, 12); //sets the different attributs
			StyleConstants.setForeground(events, temp); // for the font style
			StyleConstants.setBold(events, true);
			StyleConstants.setItalic(events, false);
		highlights = sc.addStyle("HighlightStyle", defaultStyle);	//creates a new style for highlight
			StyleConstants.setFontFamily(highlights, "Dialog");
			StyleConstants.setFontSize(highlights, 12); //sets the different attributs
			StyleConstants.setForeground(highlights, temp1); // for the font style
			StyleConstants.setBold(highlights, true);
			StyleConstants.setItalic(highlights, false);
		MOTD = sc.addStyle("MOTDStyle", defaultStyle);	//creates a new style for highlight
			StyleConstants.setFontFamily(MOTD, "Dialog");
			StyleConstants.setFontSize(MOTD, 12); //sets the different attributs
			StyleConstants.setForeground(MOTD, temp1); // for the font style
			StyleConstants.setBold(MOTD, true);
			StyleConstants.setItalic(MOTD, false);
	}

	/** returns the message style
	 * @author Dybdal
	 * @return messages
	 * @since 1.0
	 */
	public Style getMessage() {
		return messages;
	}


	/** returns the event style
	 * @author Dybdal
	 * @return events
	 * @since 1.0
	 */
	public Style getEvent() {
		return events;
	}


	/** returns the highlight style
	 * @author Dybdal
	 * @return highlights
	 * @since 1.0
	 */
	public Style getHighlight() {
		return highlights;
	}
}
